package com.sgb.zhaocai.view.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import com.sgb.link.entity.PickInvitedApply;
import com.sgb.zhaocai.R;
import com.sgb.zhaocai.databinding.PickMarketPersonalFragmentBinding;
import com.sgb.zhaocai.entity.MarketPersonalEntitly;
import com.sgb.zhaocai.entity.MemberCompanyEntitly;
import com.sgb.zhaocai.view.activity.PickInvitedApplyDetailsActivity;
import com.sgb.zhaocai.view.adapter.MarketPersonalAdapter;
import com.sgb.zhaocai.viewmodel.PickMarketPersonalModel;
import com.swgk.core.base.AppLazyFragment;
import com.swgk.core.util.DateUtils;
import com.swgk.core.util.MToast;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

/**
 * 市场客户-个人
 */
public class PickMarketPersonalFragment extends AppLazyFragment {

    private PickMarketPersonalFragmentBinding mBinding;
    private PickMarketPersonalModel mModel;
    private static String DATA = "data";
    private PickInvitedApply bean;

    private boolean isLoadMore = false;
    private int num = 0;
    private MarketPersonalAdapter marketpersonaladapter;
    private String searchnames = "";
    private static String FROMWHERE = "fromWhere";
    private String fromWhere;
    private boolean isSelectAll = false;
    private String time = "";
    private boolean isTextview = false;

    public static PickMarketPersonalFragment newInstance(PickInvitedApply pickinvitedapply, String fromWhere) {
        PickMarketPersonalFragment fragment = new PickMarketPersonalFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable(DATA,pickinvitedapply);
        bundle.putString(FROMWHERE, fromWhere);
        fragment.setArguments(bundle);
        return fragment;
    }
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.pick_market_personal_fragment, container, false);
        mModel = new ViewModelProvider(this).get(PickMarketPersonalModel.class);
        mModel.setActivity(PickMarketPersonalFragment.this);
        bean = (PickInvitedApply) getArguments().get(DATA);
        fromWhere =(String) getArguments().get(FROMWHERE);
        return mBinding.getRoot();
    }
    @Override
    protected void onLazyLoad() {
    }

    private void initData(boolean isLoadMore) {
        mModel.getFindPersonalUserByName(isLoadMore,searchnames);
    }

    @Override
    protected void initView() {
        super.initView();
        if (marketpersonaladapter == null) {
            marketpersonaladapter = new MarketPersonalAdapter(getActivity(), null);
        }
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        mBinding.recyclerView.setLayoutManager(layoutManager);
        mBinding.recyclerView.setHasFixedSize(true);
        //解决数据加载完成后, 没有停留在顶部的问题
        mBinding.recyclerView.setFocusable(false);
        mBinding.recyclerView.setAdapter(marketpersonaladapter);
        mBinding.recyclerView.setLoadingMoreEnabled(true);
        mBinding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                isLoadMore= false;
                initData(false);
            }

            @Override
            public void onLoadMore() {
                isLoadMore = true;
                //上拉加载更多
                initData(isLoadMore);
            }
        });
        marketpersonaladapter.onIChildClick(new MarketPersonalAdapter.IChildClick() {
            @Override
            public void onClick(MarketPersonalEntitly membercompanyentitly) {
                membercompanyentitly.setCheck(!membercompanyentitly.isCheck());
                marketpersonaladapter.notifyDataSetChanged();
                invitedSureData();
            }
        });
        //全选按钮
        mBinding.llCheckAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isSelectAll){
                    isSelectAll = false;
                    mBinding.checkImageview.setImageResource(R.drawable.icon_ring );
                    mBinding.invitedSure.setText("确认邀请");
                    mBinding.invitedSure.setBackground(getActivity().getResources().getDrawable(R.drawable.bg_tint_orange_5));
                    isTextview = false;
                }else {
                    isSelectAll = true;
                    mBinding.checkImageview.setImageResource(R.drawable.icon_select_item );
                    mBinding.invitedSure.setText("确认邀请("+marketpersonaladapter.getDatas().size()+")");
                    mBinding.invitedSure.setBackground(getActivity().getResources().getDrawable(R.drawable.bg_orange_5));
                    isTextview = true;
                }

                for (MarketPersonalEntitly data : marketpersonaladapter.getDatas()) {
                    data.setCheck(isSelectAll);
                }
                marketpersonaladapter.notifyDataSetChanged();
            }
        });
        //确认邀请实现
        mBinding.invitedSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isTextview){
                    invitedSureDataList();
                }
            }
        });

        //置顶
        mBinding.imgBackTop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mBinding.imgBackTop.setVisibility(View.GONE);
                mBinding.recyclerView.getLayoutManager().scrollToPosition(0);
            }
        });
    }

    public void setFindPersonalUserByName(List<MarketPersonalEntitly> data, int listSize) {
        RecyclerRefreshing();
        if (isLoadMore) {
            if (data != null && data.size() > 0) {
                marketpersonaladapter.addDatas(data);
            }
            if (listSize>=marketpersonaladapter.getDatas().size()){
                mBinding.recyclerView.setNoMore(false);
            }else {
                mBinding.recyclerView.setNoMore(true);
            }
            mBinding.imgBackTop.setVisibility(View.VISIBLE);
        } else {
            if (data != null && data.size() > 0) {
                mBinding.emptyView.setVisibility(View.GONE);
                mBinding.recyclerView.setVisibility(View.VISIBLE);
                mBinding.llBottom.setVisibility(View.VISIBLE);
                marketpersonaladapter.clearDatas();
                marketpersonaladapter.setDatas(data);
                mBinding.recyclerView.scrollToPosition(0);
                if (listSize>=marketpersonaladapter.getDatas().size()){
                    mBinding.recyclerView.setNoMore(false);
                }else {
                    mBinding.recyclerView.setNoMore(true);
                }
                mBinding.imgBackTop.setVisibility(View.GONE);
            } else {
                mBinding.imgBackTop.setVisibility(View.GONE);
                marketpersonaladapter.clearDatas();
                mBinding.emptyView.setVisibility(View.VISIBLE);
                mBinding.recyclerView.setVisibility(View.GONE);
                mBinding.llBottom.setVisibility(View.GONE);
            }
        }
        invitedSureData();
    }

    private void invitedSureData() {
        num=0;
        if (marketpersonaladapter.getDatas()!=null&&marketpersonaladapter.getDatas().size()>0){
            for (MarketPersonalEntitly datas : marketpersonaladapter.getDatas()) {
                if (!datas.isCheck()){
                    //全选按钮设置成不全选
                    if (isSelectAll){
                        isSelectAll = false;
                    }
                    mBinding.checkImageview.setImageResource(R.drawable.icon_ring );
                }else {
                    num++;
                }
            }
            if (num == 0){
                mBinding.invitedSure.setText("确认邀请");
                mBinding.invitedSure.setBackground(getActivity().getResources().getDrawable(R.drawable.bg_tint_orange_5));
                isTextview = false;
            }else {
                mBinding.invitedSure.setText("确认邀请("+num+")");
                mBinding.invitedSure.setBackground(getActivity().getResources().getDrawable(R.drawable.bg_orange_5));
                isTextview = true;
            }
        }
    }


    private void RecyclerRefreshing() {
        mBinding.recyclerView.refreshComplete();
        mBinding.recyclerView.loadMoreComplete();
    }

   public void initSearch(String SearchName) {
        this.searchnames = SearchName;
        initData(false);
    }

    //提交数据
    private void invitedSureDataList() {
        List<String> userList = new ArrayList<>();
        List<String> userNameList = new ArrayList<>();
        if (TextUtils.equals(fromWhere, "1")) {
            time =   bean.getApplyEndTime();
        } else {
            time = "";
        }
        for (MarketPersonalEntitly data : marketpersonaladapter.getDatas()) {
            if (data.isCheck()){
                userList.add(data.getNo());
                userNameList.add(data.getRealName());
            }
        }
        if (userList.size()>0){
            PickInvitedApplyDetailsActivity.start(getActivity(), fromWhere, bean.getName(), time, 0, new ArrayList<>(),bean.getId(),userList,new ArrayList<>(),userNameList,0,"");
        }else {
            //MToast.showToast(getActivity(),"请选择会员单位个人数据");
        }
    }
}
